{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "Gear Change"

// By ArtemQa146

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

const
    Check = 31@
    Check_Global = 30@
    current_gear = 29@
    gear_old = 28@
    gear_ini = 27@
    sub_cl = 26@
    car_x = 25@
end

while true
wait 0
    if
        player.Defined(0)
    then
    gosub @Check_Global
    if
        Check_Global == true
    then
    
        0441: 5@ = car 0@ model
        0AD3: 10@v = string_format "%d" 5@
        0AF0: gear_ini = get_int_from_ini_file "CLEO\Gear Change.ini" section "Gear" key 10@v
        
        0085: car_X = 0@ // (int)
    
        repeat
        wait 0
        04ED: load_animation  "DrIVeMod"
        until 04EE:   animation "DrIVeMod" loaded
        
gear_old = -1
timera = 0

while true
wait 0
    gosub @Check_Global
        if and
            003B:   car_X == 0@  // (int)
            Check_Global == true
        then
                if
                    0ABE:   car 0@ engine_on
                then
                
                    046C: 1@ = car 0@ driver
                    if
                        803B:   not 1@ == $PLAYER_ACTOR  // (int)
                    then
                        continue
                    end
                
                    // 
                    if
                        gear_ini == 3
                    then
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc1_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc2_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc3_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc4_FL"
                    then
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_L"
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_R"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_L"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_R"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_TUNE_RADIO"
                    then
                        continue
                    end
                
                
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "DRIVE_L"
                        Actor.Animation($PLAYER_ACTOR) == "DRIVE_R"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT2"
                        Actor.Animation($PLAYER_ACTOR) == "NAZADPRO"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATPRO"
                        0E42: is_char_doing_task_id $PLAYER_ACTOR task_id 1022
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "sportNAZAD"
                        Actor.Animation($PLAYER_ACTOR) == "sportVOZVRAT"
                        Actor.Animation($PLAYER_ACTOR) == "sportVOZVRAT2"
                        Actor.Animation($PLAYER_ACTOR) == "NAZADTRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATTRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT2TRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "NAZAD"
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "horn"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATPRO2"
                        Actor.Animation($PLAYER_ACTOR) == "handbrake"
                        Actor.Animation($PLAYER_ACTOR) == "handbrakeL"
                        Actor.Animation($PLAYER_ACTOR) == "wopen"
                        Actor.Animation($PLAYER_ACTOR) == "wclose"
                    then
                        timera = 0
                        continue
                    end
                    
                    0@ = Actor.CurrentCar($PLAYER_ACTOR)
                    0E12: get_vehicle 0@ subclass_to sub_cl

                    if or
                        sub_cl == 1
                        sub_cl == 0
                    then
                    
                        //   
                        if and
                            not gear_ini == 1
                            not gear_ini == 2
                            not gear_ini == 3
                            not gear_ini == 4
                        then

                            02E3: 1@ = car 0@ speed
                            if or
                                1@ < 1.0
                                01C1:   car 0@ stopped
                            then
                                gear_old = 146
                                continue
                            end

                            0@ = Actor.CurrentCar($PLAYER_ACTOR)
                            0AB8: get_car 0@ current_gear_to current_gear

                            0A97: 1@ = car 0@ struct
                            0D4E: 2@ = read_struct 1@ offset 0x284 size 1

                            if gear_old == -1
                            then 0AB8: get_car 0@ current_gear_to gear_old
                            end

                            0EFD: get_car_pedals 0@ gas_to 3@ break_to 4@

                            if and
                                timera > 300
                                not Actor.Animation($PLAYER_ACTOR) == "gear_ch"
                                803B:   not current_gear == gear_old  // (int)
                                not 4@ > 0.0
                            then
                                02E3: 1@ = car 0@ speed
                                if or
                                    1@ < 5.0
                                    2@ == 0
                                then
                                    0AB8: get_car 0@ current_gear_to gear_old
                                    0605: actor $Player_actor perform_animation "gear_ch" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    timera = 0
                                end
                            end
                        end
                        
                        // 
                        if
                            gear_ini == 4
                        then

                            02E3: 1@ = car 0@ speed
                            if or
                                1@ < 1.0
                                01C1:   car 0@ stopped
                            then
                                gear_old = 146
                                continue
                            end

                            0@ = Actor.CurrentCar($PLAYER_ACTOR)
                            0AB8: get_car 0@ current_gear_to current_gear

                            0A97: 1@ = car 0@ struct
                            0D4E: 2@ = read_struct 1@ offset 0x284 size 1

                            if gear_old == -1
                            then 0AB8: get_car 0@ current_gear_to gear_old
                            end

                            0EFD: get_car_pedals 0@ gas_to 3@ break_to 4@

                            if and
                                timera > 300
                                not Actor.Animation($PLAYER_ACTOR) == "gear_chL"
                                803B:   not current_gear == gear_old  // (int)
                                not 4@ > 0.0
                            then
                                02E3: 1@ = car 0@ speed
                                if or
                                    1@ < 5.0
                                    2@ == 0
                                then
                                    0AB8: get_car 0@ current_gear_to gear_old
                                    0605: actor $Player_actor perform_animation "gear_chL" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    timera = 0
                                end
                            end
                        end

                        //   
                        if or
                            gear_ini == 1
                            gear_ini == 2

                        then

                            02E3: 1@ = car 0@ speed
                            if or
                                1@ < 1.0
                                01C1:   car 0@ stopped
                            then
                                gear_old = 146
                                continue
                            end

                            0@ = Actor.CurrentCar($PLAYER_ACTOR)
                            0AB8: get_car 0@ current_gear_to current_gear

                            if gear_old == -1
                            then 0AB8: get_car 0@ current_gear_to gear_old
                            end

                            0EFD: get_car_pedals 0@ gas_to 3@ break_to 4@

                            if and
                                timera > 500
                                not Actor.Animation($PLAYER_ACTOR) == "gear_ch"
                                not Actor.Animation($PLAYER_ACTOR) == "gear_ch2"
                                803B:   not current_gear == gear_old  // (int)
                                not 4@ > 0.0
                                current_gear < 2
                            then
                                02E3: 1@ = car 0@ speed
                                if
                                    1@ < 3.0
                                then
                                    0AB8: get_car 0@ current_gear_to gear_old
                                    if gear_ini == 1
                                    then 0605: actor $Player_actor perform_animation "gear_ch" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    end
                                    if gear_ini == 2
                                    then 0605: actor $Player_actor perform_animation "gear_ch2" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    end

                                    timera = 0
                                end
                            end
                        end
                    
                    end
                    
                    // 
                    if and
                        not gear_ini > 0
                        sub_cl == 9
                    then
                        02E3: 1@ = car 0@ speed
                        if or
                            1@ < 3.0
                            01C1:   car 0@ stopped
                        then
                            gear_old = 146
                            continue
                        end
                        
                        
                        if or
                            Actor.Animation($PLAYER_ACTOR) == "BIKEH_Ride"
                            Actor.Animation($PLAYER_ACTOR) == "BIKED_Ride"
                            Actor.Animation($PLAYER_ACTOR) == "BIKEV_Ride"
                            Actor.Animation($PLAYER_ACTOR) == "BIKES_Ride"
                            Actor.Animation($PLAYER_ACTOR) == "WF_Ride"
                        then
                        else
                            continue
                        end
                        
                        if or
                            Actor.Animation($PLAYER_ACTOR) == "ANIM1"
                            Actor.Animation($PLAYER_ACTOR) == "ANIM2"
                            Actor.Animation($PLAYER_ACTOR) == "ANIM3"
                            Actor.Animation($PLAYER_ACTOR) == "ANIM4"
                        then
                            continue
                        end

                        0@ = Actor.CurrentCar($PLAYER_ACTOR)
                        0AB8: get_car 0@ current_gear_to current_gear

                        0A97: 1@ = car 0@ struct
                        0D4E: 2@ = read_struct 1@ offset 0x284 size 1

                        if gear_old == -1
                        then 0AB8: get_car 0@ current_gear_to gear_old
                        end

                        0EFD: get_car_pedals 0@ gas_to 3@ break_to 4@

                        if and
                            timera > 500
                            not Actor.Animation($PLAYER_ACTOR) == "gear_chB"
                            not Actor.Animation($PLAYER_ACTOR) == "gear_chB2"
                            803B:   not current_gear == gear_old  // (int)
                            not 4@ > 0.0
                            not current_gear == 0
                        then
                            02E3: 1@ = car 0@ speed
                            if
                                2@ == 0
                            then
                                0AB8: get_car 0@ current_gear_to gear_old
                                if or
                                    Actor.Animation($PLAYER_ACTOR) == "BIKEV_Ride"
                                    Actor.Animation($PLAYER_ACTOR) == "BIKEV_Right"
                                    Actor.Animation($PLAYER_ACTOR) == "BIKEV_Left"
                                then
                                    0A1A: actor $Player_actor perform_walk_animation "gear_chB" IFP "DrIVeMod" framedelta 8.0 loopA 0 lockX 0 lockY 0 lockF 0 0 ms // versionC
                                else
                                    0A1A: actor $Player_actor perform_walk_animation "gear_chB2" IFP "DrIVeMod" framedelta 8.0 loopA 0 lockX 0 lockY 0 lockF 0 0 ms // versionC
                                end
                                timera = 0
                            end
                        end
                    end
                    
                    
                end
            else
                04EF: release_animation "DrIVeMod"
                break
            end
        end
        
            
        end
    end
end

:Check_Global
Check_Global = false
    if and
        Actor.Driving($PLAYER_ACTOR)
        8741:   not actor $PLAYER_ACTOR busted
        not actor.Dead($PLAYER_ACTOR)
        not is_on_cutscene
        not is_on_scripted_cutscene
    then
        if and
            84C8:   not actor $PLAYER_ACTOR driving_flying_vehicle
            84A7:   not actor $PLAYER_ACTOR driving_boat
            84A9:   not actor $PLAYER_ACTOR driving_heli
            89AE:   not actor $PLAYER_ACTOR driving_train
        then
            0@ = Actor.CurrentCar($PLAYER_ACTOR)
            0E12: get_vehicle 0@ subclass_to 1@
            if or
                1@ == 1
                1@ == 0
                1@ == 9
            then
                Check_Global = true
            end
        end
    end
return


